import React, { Component } from 'react'
import {
  Card,
  Breadcrumb,
  Form,
  Radio,
  Button,
  DatePicker,
  Table,
  Tag,
  Modal,
  message,
} from 'antd'
import { Link } from 'react-router-dom'
import style from './index.module.scss'
import { ArticleStatus } from 'api/constants'
import Channel from 'components/Channel'
import { getArticles, delArticle } from 'api/article'
// 导入默认图片
import defaultImg from 'assets/error.png'
// 导入图标
import {
  DeleteOutlined,
  EditOutlined,
  ExclamationCircleOutlined,
} from '@ant-design/icons'
// const { Option } = Select
const { RangePicker } = DatePicker
const { confirm } = Modal
export default class index extends Component {
  // 文章列表每列渲染
  columns = [
    {
      title: '封面',
      render(data) {
        if (data.cover.type === 0) {
          return (
            <img
              src={defaultImg}
              alt=""
              style={{ width: 200, height: 120, objectFit: 'cover' }}
            />
          )
        }
        return (
          <img
            src={data.cover.images[0]}
            alt=""
            style={{ width: 200, height: 120, objectFit: 'cover' }}
          />
        )
      },
    },
    {
      title: '标题',
      // 对应data里面属性
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: (status) => {
        const obj = ArticleStatus.find((item) => status === item.id)
        return <Tag color={obj.color}>{obj.name}</Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      render: (data) => {
        return (
          <>
            {/* 修改 */}
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined />}
              onClick={() => {
                this.eidtArticle(data.id)
              }}
            ></Button>
            {/* 删除 */}
            <Button
              type="primary"
              shape="circle"
              danger
              icon={<DeleteOutlined />}
              onClick={() => {
                this.delArticle(data.id)
              }}
            ></Button>
          </>
        )
      },
    },
  ]

  // 查询文章列表参数
  reqParams = {
    page: 1,
    per_page: 10,
  }

  state = {
    // 频道列表数据
    // channels: [],
    // 文章列表数据
    articles: {},
  }

  render() {
    const { total_count, results, per_page, page } = this.state.articles
    return (
      <div className={style.root}>
        {/* 表单卡片区域 */}
        <Card
          // 头部
          title={
            // 面包屑
            <Breadcrumb>
              <Breadcrumb.Item>
                <Link to="/home">首页</Link>
              </Breadcrumb.Item>
              <Breadcrumb.Item>文章列表</Breadcrumb.Item>
            </Breadcrumb>
          }
        >
          {/* 筛选表单区域 */}
          <Form initialValues={{ status: -1 }} onFinish={this.onFinish}>
            {/* 审核状态 */}
            <Form.Item label="状态" name="status">
              <Radio.Group>
                {/* <Radio value={-1}>全部</Radio>
                <Radio value={0}>草稿</Radio>
                <Radio value={1}>待审核</Radio>
                <Radio value={2}>审核通过</Radio>
                <Radio value={3}>审核失败</Radio> */}
                {ArticleStatus.map((item) => (
                  <Radio key={item.id} value={item.id}>
                    {item.name}
                  </Radio>
                ))}
              </Radio.Group>
            </Form.Item>
            {/* 频道下拉列表 */}
            <Form.Item label="频道" name="channel_id">
              {/* <Select style={{ width: 200 }} placeholder="请选择文章频道">
                {this.state.channels.map((item) => (
                  <Option key={item.id} value={item.id}>
                    {item.name}
                  </Option>
                ))}
              </Select> */}
              <Channel></Channel>
            </Form.Item>
            {/* 日期选择 */}
            <Form.Item label="日期" name="date">
              <RangePicker />
            </Form.Item>
            {/* 按钮 */}
            <Form.Item>
              <Button type="primary" htmlType="submit">
                筛选
              </Button>
            </Form.Item>
          </Form>
        </Card>
        {/* 表格卡片区域 */}
        <Card title={`根据筛选条件共筛选到${total_count}条结果`}>
          <Table
            columns={this.columns}
            dataSource={results}
            rowKey="id"
            pagination={{
              // 位置
              position: ['bottomCenter'],
              // 总数量
              total: total_count,
              // 每页显示几条
              pageSize: per_page,
              // 页码值
              current: page,
              onChange: this.onChange,
            }}
          ></Table>
        </Card>
      </div>
    )
  }
  // 表单提交
  onFinish = (val) => {
    // 状态全部则不传statue
    if (val.status !== -1) {
      this.reqParams.status = val.status
    } else {
      delete this.reqParams.status
    }
    // 频道不为空
    if (val.channel_id !== undefined) {
      this.reqParams.channel_id = val.channel_id
    } else {
      delete this.reqParams.channel_id
    }
    // 选中时间
    if (val.date) {
      this.reqParams.begin_pubdate = val.date[0]
        .startOf('day')
        .format('YYYY-MM-DD HH-mm-ss')
      this.reqParams.end_pubdate = val.date[1]
        .endOf('day')
        .format('YYYY-MM-DD HH-mm-ss')
    } else {
      delete this.reqParams.begin_pubdate
      delete this.reqParams.end_pubdate
    }
    // 筛选要让页码初始化
    console.log(this.reqParams)
    this.reqParams.page = 1
    this.getArticleList()
  }

  componentDidMount() {
    // this.getChannelList()
    this.getArticleList()
  }

  // 获取频道列表
  // async getChannelList() {
  //   const { data: res } = await getChannel()
  //   this.setState({
  //     channels: [...res.channels],
  //   })
  // }

  // 获取文章列表
  async getArticleList() {
    const { data: res } = await getArticles(this.reqParams)
    this.setState({
      articles: { ...res },
    })
  }

  // 分页改变
  onChange = (page, pageSize) => {
    this.reqParams.page = page
    this.reqParams.per_page = pageSize
    this.getArticleList()
  }

  // 删除文章
  delArticle = (id) => {
    confirm({
      title: '温馨提示',
      icon: <ExclamationCircleOutlined />,
      content: '是否确定删除这篇文章',
      onOk: async () => {
        await delArticle(id)
        message.success('删除成功')
        this.getArticleList()
      },
    })
  }

  // 修改文章
  eidtArticle = (id) => {
    this.props.history.push(`/home/publish/${id}`)
  }
}
